﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.Win32;
using System.Diagnostics;

namespace CSP.Package.Controls
{
    public partial class PostInstallationSteps : UserControl, IInstallationControl
    {
        private const string _executionPath = @"PostImplementation\PostImplementationSteps.bat";

        public PostInstallationSteps()
        {
            InitializeComponent();
        }

        #region IInstallationControl Members

        private Installer _form;
        public Installer GetForm()
        {
            return _form;
        }

        public void SetForm(Installer installer)
        {
            _form = installer;
        }

        public bool Next()
        {
            int setupModality = Convert.ToInt16(RegistryAccess.GetStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "SetupModality"));
            if (setupModality != 1)
            {
                Process proces = new Process();
                proces.StartInfo.FileName = @"CRMConnector\CrmConnector.bat";
                string arguments = string.Format(@"{0}", new string[] { GetForm().IntranetBaseUrl });

                GetForm().AddToLog("   " + labels.CspInstallLog_Arguments + ": " + arguments);

                proces.StartInfo.Arguments = arguments;
                proces.StartInfo.UseShellExecute = false;
                proces.StartInfo.RedirectStandardOutput = true;
                proces.StartInfo.RedirectStandardError = true;
                proces.Start();
            }
            // This code is commented due to a bug that causes the config files changes replication
            // across different web applications.

            return true;
            //// Cleaning up registry variables
            //GetForm().AddToLog("----------------------------------------------");
            //GetForm().AddToLog(CSP.Package.labels.CspInstallLog_PostImplementationStepsStart);

            //Process proces = new Process();
            //proces.StartInfo.FileName = _executionPath;
            //string arguments = string.Format(@"{0} {1} {2}", new string[] { GetForm().InternetBaseUrl, GetForm().IntranetBaseUrl, GetForm().CentralBaseUrl});

            //GetForm().AddToLog("   "+labels.CspInstallLog_Arguments+": " + arguments);

            //proces.StartInfo.Arguments = arguments;
            //proces.StartInfo.UseShellExecute = false;
            //proces.StartInfo.RedirectStandardOutput = true;
            //proces.StartInfo.RedirectStandardError = true;
            //proces.Start();
            //string error = proces.StandardError.ReadToEnd();
            //string output = proces.StandardOutput.ReadToEnd();
            //proces.WaitForExit();
            //if (error != string.Empty)
            //{
            //    throw new ApplicationException(error);
            //}
            //GetForm().AddToLog(output);

            //RegistryAccess.DeleteStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "CSPInternetWebApplication");
            //RegistryAccess.DeleteStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "CSPInternetWebApplication");
            //RegistryAccess.DeleteStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "CentralAdminUrl");
            
            //return true;
            
        }

        public void Previous()
        {
            
        }

        public void Skip()
        {
            
        }

        #endregion

        private string FbaConnectionString
        {
            get
            {
                if (RegistryAccess.PropertyExists(Registry.LocalMachine, Constants.CspRegistryKey, "FBAConnectionString"))
                    return RegistryAccess.GetStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "FBAConnectionString");
                else
                    return "Data Source=(local);Initial Catalog=aspnetdb;Trusted_Connection=yes;";
            }
        }

        private string WCFEndPointURL
        {
            get
            {
                if (RegistryAccess.PropertyExists(Registry.LocalMachine, Constants.CspRegistryKey, "WCFEndPointURL"))
                    return RegistryAccess.GetStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "WCFEndPointURL");
                else
                    return "http://localhost/MembershipServiceClass.svc";
            }
        }

        private string WCFServicePrincipalName
        {
            get
            {
                if (RegistryAccess.PropertyExists(Registry.LocalMachine, Constants.CspRegistryKey, "WCFServicePrincipalName"))
                    return RegistryAccess.GetStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "WCFServicePrincipalName");
                else
                    return "host/localhost";
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            richTextBox1.Text = richTextBox1.Text.Replace("{0}", FbaConnectionString);
            richTextBox1.Text = richTextBox1.Text.Replace("{1}", WCFEndPointURL);
            richTextBox1.Text = richTextBox1.Text.Replace("{2}", WCFServicePrincipalName);
        }
        private void PostInstallationSteps_Load(object sender, EventArgs e)
        {

        }
    }
}
